﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Author: toanvt
/// CreatedDate: 13/12/2011
/// Description: Danh mục tin tức Provider
/// </summary>
namespace EHTWebsite.Code
{
    public class DanhMucTinTucProvider
    {
        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Insert, Update Danh mục tin tức
        /// </summary>
        /// <param name="theDanhMucTinTuc">Đối tượng Danh mục</param>
        /// <param name="IsAdd">trang thái thêm mới hay không</param>
        public static Int32 IUDanhMucTinTuc(DanhMucTinTuc theDanhMucTinTuc, bool isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUDanhMucTinTuc";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theDanhMucMenusParameter = new SqlParameter();

            theDanhMucMenusParameter.ParameterName = "@DanhMucTinTucIDOut";
            theDanhMucMenusParameter.Direction = ParameterDirection.Output;
            theDanhMucMenusParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucMenusParameter);

            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", theDanhMucTinTuc.DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@TenDanhMucTinTucVN", theDanhMucTinTuc.TenDanhMucTinTucVN));
            command.Parameters.Add(new SqlParameter("@TenDanhMucTinTucOther", theDanhMucTinTuc.TenDanhMucTinTucOther));
            command.Parameters.Add(new SqlParameter("@ChuyenMucChaID", theDanhMucTinTuc.ChuyenMucChaID));
            command.Parameters.Add(new SqlParameter("@SapXep", theDanhMucTinTuc.SapXep));           
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@DanhMucTinTucIDOut"].Value.ToString());
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Xóa chuyên mục theo mã chuyên mục
        /// </summary>
        /// <param name="danhMucTinTucID">Mã chuyên mục</param>
        public static void DeleteDanhMucTinTucByDanhMucID(Int32 danhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteDanhMucTinTucByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", danhMucTinTucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// lay danh muc theo loai danh muc
        /// </summary>
        /// <param name="TypeDanhMuc"></param>
        /// <returns></returns>
        public static DanhMucTinTuc getDanhMucByLoaiDanhMuc(Int32 TypeDanhMuc)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucTinTucByType";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TypeDanhMuc", TypeDanhMuc));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            DanhMucTinTuc theDanhMucTinTuc = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theDanhMucTinTuc = new DanhMucTinTuc();
                theDanhMucTinTuc.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theDanhMucTinTuc.TenDanhMucTinTucVN = ds.Tables[0].Rows[0]["TenDanhMucTinTucVN"].ToString();
                theDanhMucTinTuc.TenDanhMucTinTucOther = ds.Tables[0].Rows[0]["TenDanhMucTinTucOther"].ToString();
                theDanhMucTinTuc.ChuyenMucChaID = Int32.Parse(ds.Tables[0].Rows[0]["ChuyenMucChaID"].ToString());
                theDanhMucTinTuc.TrangThai = Boolean.Parse(ds.Tables[0].Rows[0]["TrangThai"].ToString());
                theDanhMucTinTuc.LuotTruyCap = Int32.Parse(ds.Tables[0].Rows[0]["LuotTruyCap"].ToString());
                theDanhMucTinTuc.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theDanhMucTinTuc.DanhMucTinTucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucTinTucID"].ToString());
                theDanhMucTinTuc.TypeDanhMuc = Int32.Parse(ds.Tables[0].Rows[0]["TypeDanhMuc"].ToString());                
            }

            return theDanhMucTinTuc;
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Lấy danh sách chuyên mục tin tức
        /// </summary>
        /// <returns>Danh sách chuyên mục</returns>
        public static DataSet GetDanhMucTinTucsByNhomDanhMucID(Int32 nhomDanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucTinTucsByNhomDanhMucID";
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", nhomDanhMucID));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }


        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Lấy danh sách chuyên mục tin tức theo chuyen muc cha
        /// </summary>
        /// <returns>Danh sách chuyên mục</returns>
        public static DataSet GetDanhMucTinTucsByParentID(Int32 nhomDanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetDanhMucTinTucsByParentID]";
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", nhomDanhMucID));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }


        public static DataSet GetDanhMucTinTucsFillter(Int32 DanhMucTinTucID, String TieuDeVN, String TieuDeOther)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucTinTucsFillter";
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@TenDanhMucTinTucVN", TieuDeVN));
            command.Parameters.Add(new SqlParameter("@TenDanhMucTinTucOther", TieuDeOther));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }
        

        public static DataSet GetTinTucsByDanhMucTinTucID(Int32 DanhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));

            return DataProvider.ExecuteDataSet(command);
        }

        public static DataSet GetDanhMucTinTuc()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetDanhMucTinTuc]";
            command.CommandType = CommandType.StoredProcedure;            

            return DataProvider.ExecuteDataSet(command);
        }

        public static void changePublish(Int32 DanhMucTinTucID, Boolean publish, String Icon)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_ChangePublishByDanhMucTinTucID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@Icon", Icon));
            command.Parameters.Add(new SqlParameter("@TrangThai", publish));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateLuotTruyCap(Int32 DanhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UDanhMucTinTuc_LuotTruyCap";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Lấy chuyên mục theo mã chuyên mục
        /// </summary>
        /// <param name="danhMucTinTucID">Mã chuyên mục</param>
        /// <returns>Đối tượng chuyên muc</returns>
        public static DanhMucTinTuc GetDanhMucTinTucByDanhMucID(Int32 danhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucTinTucByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", danhMucTinTucID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            DanhMucTinTuc theDanhMucTinTuc = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theDanhMucTinTuc = new DanhMucTinTuc();
                theDanhMucTinTuc.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theDanhMucTinTuc.TenDanhMucTinTucVN = ds.Tables[0].Rows[0]["TenDanhMucTinTucVN"].ToString();
                theDanhMucTinTuc.TenDanhMucTinTucOther = ds.Tables[0].Rows[0]["TenDanhMucTinTucOther"].ToString();
                theDanhMucTinTuc.ChuyenMucChaID = Int32.Parse(ds.Tables[0].Rows[0]["ChuyenMucChaID"].ToString());
                theDanhMucTinTuc.TrangThai = Boolean.Parse(ds.Tables[0].Rows[0]["TrangThai"].ToString());
                theDanhMucTinTuc.LuotTruyCap = Int32.Parse(ds.Tables[0].Rows[0]["LuotTruyCap"].ToString());
                theDanhMucTinTuc.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theDanhMucTinTuc.DanhMucTinTucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucTinTucID"].ToString());
                theDanhMucTinTuc.TypeDanhMuc = Int32.Parse(ds.Tables[0].Rows[0]["TypeDanhMuc"].ToString());                
            }

            return theDanhMucTinTuc;
        }
    }
}